/**
 * Copyright (C) Study Information Technology, LTD. 
 * All Rights Reserved.
 *
 * SpringBeanUtil.java created on 2019年5月16日 上午10:01:12 by Lyon Lu 
 */
package com.mvc.server.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * <pre>
 * Description:
 * 
 * @author Lyon Lu
 * @date 2019年5月16日 上午10:01:12
 *
 * </pre>
 */
@Component
public class SpringContextUtil implements ApplicationContextAware
{

    private static ApplicationContext applicationContext;
    
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
    {
        SpringContextUtil.applicationContext = applicationContext;
    }
    
    /**
     * Description:
     * 		获取ApplicationContext
     * @return ApplicationContext      
     * @throws
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * Description:
     * 		通过name获取Bean
     * @param name
     * @return Object      
     * @throws
     */
    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }

    /**
     * Description:
     * 		通过class获取Bean
     * @param clazz
     * @return T      
     * @throws
     */
    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

    /**
     * Description:
     * 		通过name,以及class返回指定的Bean
     * @param name
     * @param clazz
     * @return T      
     * @throws
     */
    public static <T> T getBean(String name, Class<T> clazz) {
        return getApplicationContext().getBean(name, clazz);
    }
}
